package de.hpi.generator.model.dependencies;

import java.util.ArrayList;

import de.hpi.generator.model.Column;

public class InclusionDependencyCollection extends ArrayList<InclusionDependency> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * checks if the column is dependent on aDependentColumn
     * 
     * @param aDependentColumn
     * @return true if dependent, otherwise false
     */
    public boolean isDependentOn(Column aDependentColumn) {
        for (InclusionDependency aInclusionDependency : this) {
            for (Column aColumn : aInclusionDependency.getDependentColumns()) {
                if (aColumn == aDependentColumn
                        || aColumn.isDependentOn(aDependentColumn)) {
                    return true;                    
                }
            }
        }
        return false;
    }

    /**
     * generates data for dependent columns
     * 
     * @param aDataStorage
     * @throws Exception
     */
    public void generateDependentColumnsData() throws Exception {
        for (InclusionDependency aInclusionDependency : this) {
            aInclusionDependency.generateDependentData();
        }
    }

    @Override
    public boolean add(InclusionDependency aInclusionDependency) {
        if (hasAnyCompositeDependency()) {
            throw new UnsupportedOperationException("Cannot add the inclusion dependency"+ aInclusionDependency);
        }
        return super.add(aInclusionDependency);
    }

    protected boolean hasAnyCompositeDependency() {
        for (InclusionDependency aInclusionDependency : this) {
            if(aInclusionDependency instanceof CompositeInclusionDependency)
                return true;
        }
        return false;
    }
    
}
